const app = getApp()
const date = new Date()
const years = []
const months = []
const days = []
const weekstarts = []
const weekends = []


for (let i = 1; i <= 20; i++) {
  weekstarts.push(i)
}
for (let i = 1; i <= 20; i++) {
  weekends.push(i)
}

Page({

  /**
   * 页面的初始数据
   */
  data: {

    hideFlag: true,//true-隐藏  false-显示
    classname:"",
    weekDays: [1,2,3,4,5,6,7],
    weekDay: 0,
    courseTimes:[1,2,3,4,5,6,7,8,9,10,11,12],
    courseTime: 0,
    times:[1,2,3,4],
    time: 0,
    weekstarts: weekstarts,
    weekstart: 1,
    weekends: weekends,
    weekend: 1,
    value: [0, 1, 1],
    campuses: ['金石滩', '开发区'],
    campus: '金石滩',
    building: "",
    classroom:"",
    
  },
  onLoad:function(){
      var that=this;
      var username = wx.getStorageSync("username")
      that.setData({
          username:username
      })
      wx.setNavigationBarTitle({
          title: "添加课程",
      });
  },
  
  onaddCourse:function(e){
    var that=this;
    wx.request({
         url: app.globalData.targetUrl+ 'users/class/',
          method: 'POST',
          header: {
                "content-type": "application/x-www-form-urlencoded" 
          },
          data: {
            "username": that.data.username,
            "classname":that.data.classname,
            "weekstart":that.data.weekstart,
            "weekend":that.data.weekend,
            "weekDay":that.data.weekDay,
            "courseTime":that.data.courseTime,
            "time":that.data.time,
            "campus":that.data.campus,
            "building":that.data.building,
            "classroom":that.data.classroom,
            "is_redis": 1
          },
          success: function (res){
              wx.showModal({
                title: '提示',
                content: '添加成功',
                showCancel: false,
                success: res => {
                  if(res.confirm){
                    wx.switchTab({
                      url: "/pages/classable/classable"
                    })
                  }
                }
              })
          }
    })
  },
  bindChangeday: function (e) {
    const val = e.detail.value
    this.setData({
      weekDay: this.data.weekDays[val[0]],
      courseTime: this.data.courseTimes[val[1]],
      time: this.data.times[val[2]]
    })
  },
  showModalday: function () {
    var that = this;
    that.setData({
      whichshow: 0
    })
    that.showModal();
  },
  bindChangeweek: function (e) {
    console.log(e)
    const val = e.detail.value
    if (this.data.weekends[val[1]] < this.data.weekstarts[val[0]]){
      this.setData({
        weekstart: this.data.weekstarts[val[0]],
        weekend: this.data.weekends[val[0]],
      })
    }
    else{
      this.setData({
        weekstart: this.data.weekstarts[val[0]],
        weekend: this.data.weekends[val[1]],
      })
    }
  },
  showModalweek: function () {
    var that = this;
    that.setData({
      whichshow: 1
    })
    that.showModal();
  },
  bindChangecampus: function (e) {
    console.log(e)
    const val = e.detail.value
    this.setData({
      campus: this.data.campuses[val[0]],
    })
  },
  showModalcampus: function () {
    var that = this;
    that.setData({
      whichshow: 2
    })
    that.showModal();
  },
  bindinputclassarea: function (e) {
    const val = e.detail.value
    this.setData({
      building:val
    })
  },
  bindinputclassroom: function (e) {
    var that = this;
    that.setData({
      classroom: e.detail.value
    })
  },
  bindinputclassname: function (e) {
    var that = this;
    that.setData({
      classname: e.detail.value
    })
  },
  /*// 点击选项
  getOption: function (e) {
    console.log(e)
    var that = this;
    that.setData({
      value: e.currentTarget.dataset.value,
      hideFlag: true
    })
  },
  //取消
  mCancel: function () {
    var that = this;
    that.hideModal();
  },*/

  // ----------------------------------------------------------------------modal
  // 显示遮罩层
  showModal: function () {
    var that = this;
    that.setData({
      hideFlag: false,
    })
    // 创建动画实例
    var animation = wx.createAnimation({
      duration: 400,//动画的持续时间
      timingFunction: 'ease',//动画的效果 默认值是linear->匀速，ease->动画以低速开始，然后加快，在结束前变慢
    })
    this.animation = animation; //将animation变量赋值给当前动画
    var time1 = setTimeout(function () {
      that.slideIn();//调用动画--滑入
      clearTimeout(time1);
      time1 = null;
    }, 100)
  },

  // 隐藏遮罩层
  hideModal: function () {
    var that = this;
    var animation = wx.createAnimation({
      duration: 400,//动画的持续时间 默认400ms
      timingFunction: 'ease',//动画的效果 默认值是linear
    })
    this.animation = animation
    that.slideDown();//调用动画--滑出
    var time1 = setTimeout(function () {
      that.setData({
        hideFlag: true
      })
      clearTimeout(time1);
      time1 = null;
    }, 220)//先执行下滑动画，再隐藏模块

  },
  //动画 -- 滑入
  slideIn: function () {
    this.animation.translateY(0).step() // 在y轴偏移，然后用step()完成一个动画
    this.setData({
      //动画实例的export方法导出动画数据传递给组件的animation属性
      animationData: this.animation.export()
    })
  },
  //动画 -- 滑出
  slideDown: function () {
    this.animation.translateY(300).step()
    this.setData({
      animationData: this.animation.export(),
    })
  },
  

})